{
  "cells": [
    {
      "metadata": {
        "id": "zWgEkOAO9OVz"
      },
      "cell_type": "markdown",
      "source": [
        "# Modular launch notebook.\n",
        "\n",
        "This notebook can be used as an alternative to launch.py.\n",
        "\n"
      ]
    },
    {
      "metadata": {
        "id": "esFO3miE3s41"
      },
      "cell_type": "markdown",
      "source": [
        "<a href=\"https://colab.research.google.com/github/google-deepmind/concordia/blob/main/examples/deprecated/modular/notebook.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "metadata": {
        "id": "J2TwJrZ08wXz"
      },
      "cell_type": "markdown",
      "source": [
        "## Setup and imports"
      ]
    },
    {
      "metadata": {
        "cellView": "form",
        "id": "RIdEMGMLC-Hr"
      },
      "cell_type": "code",
      "source": [
        "# @title Colab-specific setup (use a CodeSpace to avoid the need for this).\n",
        "try:\n",
        "  %env COLAB_RELEASE_TAG\n",
        "except:\n",
        "  pass  # Not running in colab.\n",
        "else:\n",
        "  %pip install --ignore-requires-python --requirement 'https://raw.githubusercontent.com/google-deepmind/concordia/main/examples/requirements.in' 'git+https://github.com/google-deepmind/concordia.git#egg=gdm-concordia'\n",
        "  %pip list"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "-qLG5ExLqpWa"
      },
      "cell_type": "code",
      "source": [
        "import datetime\n",
        "import importlib\n",
        "\n",
        "from concordia.contrib import language_models as utils\n",
        "from concordia.language_model import call_limit_wrapper\n",
        "from concordia.utils.deprecated import measurements as measurements_lib\n",
        "import sentence_transformers"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "L6YO41FyuwOO"
      },
      "cell_type": "markdown",
      "source": [
        "## Parameters"
      ]
    },
    {
      "metadata": {
        "id": "V4peKcaQuwOP"
      },
      "cell_type": "code",
      "source": [
        "# @title Parameters (edit this cell)\n",
        "\n",
        "# Pick AGENT_NAME from the factories implemented in concordia/factory/agent.\n",
        "AGENT_NAME = 'rational_agent'\n",
        "# Pick ENVIRONMENT_NAME from the factories in concordia/factory/environment.\n",
        "ENVIRONMENT_NAME = 'forbidden_fruit'\n",
        "# Pick API_TYPE from concordia/language_model/utils.py, e.g. mistral.\n",
        "API_TYPE = 'mistral'\n",
        "# Add your API key here or alternatively, leave this as None to get it from an\n",
        "# environment variable.\n",
        "API_KEY = None\n",
        "# Pick  a specific model e.g. gpt-4o if API_TYPE is openai, codestral-latest if\n",
        "# API_TYPE is mistral. See the corresponding wrapper in concordia/language_model\n",
        "# for links to the websites where the model names are listed for each API_TYPE.\n",
        "MODEL_NAME = 'codestral-latest'\n",
        "# Select an embedder by specifying one of the sentence transformer embedding\n",
        "# models listed at https://huggingface.co/sentence-transformers.\n",
        "EMBEDDER_NAME = 'all-mpnet-base-v2'\n",
        "# To debug without spending money on API calls, set DISABLE_LANGUAGE_MODEL=True.\n",
        "DISABLE_LANGUAGE_MODEL = False"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "o8G9o40NuwOP"
      },
      "cell_type": "markdown",
      "source": [
        "## Load the agent config with importlib"
      ]
    },
    {
      "metadata": {
        "id": "TvZYDlOmuwOP"
      },
      "cell_type": "code",
      "source": [
        "# @title Load the agent config with importlib\n",
        "\n",
        "IMPORT_AGENT_BASE_DIR = 'concordia.factory.agent'\n",
        "agent_module = importlib.import_module(f'{IMPORT_AGENT_BASE_DIR}.{AGENT_NAME}')\n",
        "# Load the environment config with importlib\n",
        "IMPORT_ENV_BASE_DIR = 'examples.deprecated.modular.environment'\n",
        "simulation = importlib.import_module(\n",
        "    f'{IMPORT_ENV_BASE_DIR}.{ENVIRONMENT_NAME}'\n",
        ")"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "brdgSD2NuwOQ"
      },
      "cell_type": "markdown",
      "source": [
        "## Language Model setup"
      ]
    },
    {
      "metadata": {
        "id": "Ez6153pSuwOQ"
      },
      "cell_type": "code",
      "source": [
        "# @title Language Model setup\n",
        "\n",
        "model = utils.language_model_setup(\n",
        "    api_type=API_TYPE,\n",
        "    model_name=MODEL_NAME,\n",
        "    api_key=API_KEY,\n",
        "    disable_language_model=DISABLE_LANGUAGE_MODEL,\n",
        ")"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "sb_VFDtvuwOQ"
      },
      "cell_type": "markdown",
      "source": [
        "## Setup sentence encoder"
      ]
    },
    {
      "metadata": {
        "id": "UE-enMPMuwOQ"
      },
      "cell_type": "code",
      "source": [
        "# @title Setup sentence encoder\n",
        "_embedder_model = sentence_transformers.SentenceTransformer(\n",
        "    f'sentence-transformers/{EMBEDDER_NAME}'\n",
        ")\n",
        "embedder = lambda x: _embedder_model.encode(x, show_progress_bar=False)"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "FTZjLHpYuwOQ"
      },
      "cell_type": "markdown",
      "source": [
        "# The simulation"
      ]
    },
    {
      "metadata": {
        "id": "bNl5UpuHuwOQ"
      },
      "cell_type": "markdown",
      "source": [
        "## Initialize the simulation"
      ]
    },
    {
      "metadata": {
        "id": "pCfIjcukuwOQ"
      },
      "cell_type": "code",
      "source": [
        "# @title Initialize the simulation\n",
        "measurements = measurements_lib.Measurements()\n",
        "runnable_simulation = simulation.Simulation(\n",
        "    model=model,\n",
        "    embedder=embedder,\n",
        "    measurements=measurements,\n",
        "    agent_module=agent_module,\n",
        "    override_agent_model=call_limit_wrapper.CallLimitLanguageModel(model),\n",
        ")"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "8f0j8s-_uwOR"
      },
      "cell_type": "markdown",
      "source": [
        "## Run the simulation"
      ]
    },
    {
      "metadata": {
        "id": "M4Z1ttTfuwOR"
      },
      "cell_type": "code",
      "source": [
        "# @title Run the simulation\n",
        "_, results_log = runnable_simulation()"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "oaiIye0KuwOR"
      },
      "cell_type": "markdown",
      "source": [
        "## Save the results log"
      ]
    },
    {
      "metadata": {
        "id": "sDGkKpsruwOR"
      },
      "cell_type": "code",
      "source": [
        "# @title Write the results log as an HTML file in the current working directory.\n",
        "filename = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + '.html'\n",
        "file_handle = open(filename, 'a')\n",
        "file_handle.write(results_log)\n",
        "file_handle.close()"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "qWDqd4ByzSsT"
      },
      "cell_type": "markdown",
      "source": [
        "```\n",
        "Copyright 2023 DeepMind Technologies Limited.\n",
        "\n",
        "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "you may not use this file except in compliance with the License.\n",
        "You may obtain a copy of the License at\n",
        "\n",
        "    https://www.apache.org/licenses/LICENSE-2.0\n",
        "\n",
        "Unless required by applicable law or agreed to in writing, software\n",
        "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "See the License for the specific language governing permissions and\n",
        "limitations under the License.\n",
        "```"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "last_runtime": {
        "build_target": "",
        "kind": "private"
      },
      "private_outputs": true,
      "provenance": [],
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
